/**
 * <p>Title: PostServiceImpl.java</p>
 * <p>Description: DoNotes</p>
 * @author WangJun
 * @date 2009/11/15 20:13:22
 * @version 1.0
 */
package net.donotes.blog.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.donotes.blog.dao.CategoryDao;
import net.donotes.blog.dao.PostDao;
import net.donotes.blog.dao.TagDao;
import net.donotes.blog.pojo.Category;
import net.donotes.blog.pojo.Post;
import net.donotes.blog.pojo.Tag;
import net.donotes.util.Constants;

/**
 * @author WangJun
 *
 */
public class PostService {

    private static final long serialVersionUID = 1L;
    private PostDao postDao = new PostDao();
    private TagDao tagDao = new TagDao();
    private CategoryDao categoryDao = new CategoryDao();

	/* (non-Javadoc)
	 * @see net.donotes.blog.service.PostService#addPost(net.donotes.blog.pojo.Post)
	 */
	public void addPost(Post post) {
		postDao.addPost(post);

		Tag tag = null;
		for (String tagName : post.getTags()) {
			if (!tagName.trim().equals("")) {
				tag = tagDao.getTag(tagName);
				if (tag != null) {
					tag.setFrequency(tag.getFrequency() + 1);
					tagDao.updTag(tag);
				} else {
					// TODO slug
					tagDao.addTag(new Tag(tagName, tagName, 1));
				}
			}
		}
		
		// TODO
		if (post.getCategory() != null && !post.getCategory().trim().equals("")) {
			Category category = categoryDao.getCategory(post.getCategory());
			//if (category != null) {
				category.setCounter(category.getCounter() + 1);
				categoryDao.updCategory(category);
			//} else {
			//	categoryDao.addCategory(new Category(post.getCategory(), 1));
			//}
		}
	}

	/* (non-Javadoc)
	 * @see net.donotes.blog.service.PostService#delPost(net.donotes.blog.pojo.Post)
	 */
	public void delPost(Post post) {
		postDao.delPost(post);
	}
	
	/* (non-Javadoc)
	 * @see net.donotes.blog.service.PostService#getPost(net.donotes.blog.pojo.Post)
	 */
	public Post getPost(Long postId) {
		return postDao.getPost(postId);
	}
	
	public Post getPrePost(Post post) {
		String query = "select from " + Post.class.getName() + " where createDate < createDateParam parameters java.util.Date createDateParam order by createDate desc range 0,1";
		Map<String, Date> parameters = new HashMap<String, Date>();
		parameters.put("createDateParam", post.getCreateDate());
		List<Post> posts= postDao.exeQuery(query, parameters);
		
		if (posts.iterator().hasNext()) {
			return posts.get(0);
		} else {
			return null;
		}
	}
	
	public Post getNextPost(Post post) {
		String query = "select from " + Post.class.getName() + " where createDate > createDateParam parameters java.util.Date createDateParam order by createDate asc range 0,1";
		Map<String, Date> parameters = new HashMap<String, Date>();
		parameters.put("createDateParam", post.getCreateDate());
		List<Post> posts= postDao.exeQuery(query, parameters);
		
		if (posts.iterator().hasNext()) {
			return posts.get(0);
		} else {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see net.donotes.blog.service.PostService#listPost()
	 */
	public List<Post> listPost() {
		// TODO Auto-generated method stub
		return postDao.listPost();
	}
	
	/* (non-Javadoc)
	 * @see net.donotes.blog.service.PostService#listPost()
	 */
	public List<Post> listPost(int pageNum, String category) {
		long from = (pageNum - 1) * Constants.PAGE_SIZE;
		long to = pageNum * Constants.PAGE_SIZE;		
		return postDao.listPost(from, to, category);
	}	

	/* (non-Javadoc)
	 * @see net.donotes.blog.service.PostService#updPost(net.donotes.blog.pojo.Post)
	 */
	public void updPost(Post post) {
		postDao.updPost(post);
	}
	
	
}